package com.chwn.curiousdiary.ui.main.presenter;


import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.bean.BannerBean;
import com.chwn.curiousdiary.bean.StoryListBean;
import com.chwn.curiousdiary.bean.StoryTypeBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.ui.main.contract.HomeContract;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.mvp.rx.RxSchedulers;

import java.util.List;

import rx.Observer;

/**
 * Created by Myshikin on 2017/3/20.
 */
public class HomePresenter extends HomeContract.Presenter {

    private String[]                               titles             = {"生活奥秘", "童话城堡", "奇幻传说", "国学经典", "萌趣生物"};
    /**
     * 轮播图
     */
    private Observer<HttpResult<List<BannerBean>>> homeBannerObserver = new Observer<HttpResult<List<BannerBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<BannerBean>> httpResult) {
            List<BannerBean> data = httpResult.getResdata();
            mView.showBannerList(data);
        }
    };

    /**
     * 首页故事分类
     */
    private Observer<HttpResult<List<StoryTypeBean>>> storyTypeObserver = new Observer<HttpResult<List<StoryTypeBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {

        }


        @Override
        public void onNext(HttpResult<List<StoryTypeBean>> httpResult) {
            List<StoryTypeBean> data = httpResult.getResdata();
          /*  for (int i = 0; i < data.size(); i++) {
                data.get(i).setStoryPackageDetailTitle(titles[i]);
                data.get(i).setStoryPackageTitle(titles[i]);
            }
            data.add(0 ,new StoryTypeBean("奇趣冒险" ,"奇趣冒险" , "奇趣冒险" ,"奇趣冒险" ,5));*/
            mView.showStoryType(data);
        }
    };

    /**
     * 猜你喜欢
     */
    private Observer<HttpResult<List<StoryListBean>>> homeObserver = new Observer<HttpResult<List<StoryListBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<StoryListBean>> httpResult) {
            List<StoryListBean> data = httpResult.getResdata();
            if (httpResult.getResult() == 1) {
                mView.showGuessLikeList(data);
            } else {
                //                mView.onRequestError(new HttpResultException(2, "数据为空"));
            }
        }
    };


    @Override
    public void getBannerList() {

        NetWork.getInstance()
                .getHomeApi()
                .getBannerList(Constant.OSTYPE)
                .compose(RxSchedulers.<HttpResult<List<BannerBean>>>io_main())
                .subscribe(homeBannerObserver);
    }

    @Override
    public void getHomeList(int page) {
        User user = DiaryApplication.mInstance.getmUser();

        String sex = "1";
        String babybirth = "1330531200";

        if (user != null) {
            sex = user.getBabysex();
            babybirth = user.getBabybirth();
        }
        NetWork.getInstance()
                .getHomeApi()
                //              .getHomeList(Constant.PAGE_COUNT, page, Constant.OSTYPE, sex, babybirth)
                .getHomeList(Constant.PAGE_COUNT, DiaryApplication.mInstance.getUserToken(), page, Constant.OSTYPE)
                .compose(RxSchedulers.<HttpResult<List<StoryListBean>>>io_main())
                .subscribe(homeObserver);
    }

    /**
     * 根据需求复活此方法 --- 成功复活
     */
    @Override
    public void getStoryType() {
        NetWork.getInstance()
                .getHomeApi()
                .getStoryClassifyList(Constant.PAGE_COUNT, 1, Constant.OSTYPE, "story_type")
                .compose(RxSchedulers.<HttpResult<List<StoryTypeBean>>>io_main())
                .subscribe(storyTypeObserver);
    }
}
